<template>
  <el-dialog
    ref="userConfirmLayer"
    width="400px"
    :title="params.isAdmin ? '管理员密码确认' : '用户确认'"
    :visible.sync="params.visible"
    :append-to-body="true"
  >
    <el-form ref="form" label-width="80px" :model="formData">
      <el-form-item v-show="params.isAdmin" label="用户" label-width="50px">
        <el-input v-model="formData.username" />
      </el-form-item>
      <el-form-item label="密码" label-width="50px">
        <el-input v-model="formData.pass" type="password" />
      </el-form-item>
    </el-form>
    <dialog-btn slot="footer" />
  </el-dialog>
</template>
<script>
import { test2 } from '@/api/base'
import { webUserVerify } from '@/api/user'
import Global from '@/../public/js/global.js'
import { ParamArr } from '@/../public/js/paramarr'
import DialogBtn from './DialogBtn.vue'

export default {
  name: 'UserConfirmLayer',
  components: { DialogBtn },
  props: {
    params: {
      type: Object,
      default() {
        return {
          visible: false,
          isAdmin: false // 是否为管理员
        }
      }
    }
  },
  data() {
    return {
      formData: {
        username: '',
        pass: ''
      },
      DESkey: ''
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.$showLoading(
        '正在加载数据...',
        this.$refs.userConfirmLayer.$el.querySelector('div'),
        { background: 'rgba(254, 254, 254, 0.3)' }
      )
      test2(new ParamArr())
        .then((vPar) => {
          this.DESkey = vPar.getStr('key')
          this.$closeLoading()
        })
        .catch(() => {
          this.$closeLoading()
        })
    },
    confirm() {
      if (this.formData.pass.trim() === '') {
        return this.$message({
          message: '密码不能为空，请输入',
          type: 'warning'
        })
      }
      const vPar = new ParamArr()
      if (this.params.isAdmin) vPar.setStr('User', 'admin')
      vPar.setStr(
        'Pass',
        Global.encryptByDES(Global.MD5(this.formData.pass), this.DESkey)
      )
      vPar.setStr(
        'PassDES',
        Global.encryptByDES(this.formData.username, this.DESkey)
      )
      this.$showLoading(
        '正在验证密码...',
        this.$refs.userConfirmLayer.$el.querySelector('div'),
        { background: 'rgba(254, 254, 254, 0.3)' }
      )
      webUserVerify(vPar)
        .then(() => {
          if (this.params.confirm !== undefined) {
            this.params.confirm()
          } else {
            this.$emit('confirm', '')
          }
          this.$closeLoading()
        })
        .catch(() => {
          this.$closeLoading()
        })
    }
  }
}
</script>
